//
//  ClassRoomViewController.m
//  MagicCube
//
//  Created by 蓓蕾 on 2022/2/18.
//

#import "ClassRoomViewController.h"
#import "VideoViewController.h"

@interface ClassRoomViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation ClassRoomViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    [self getDataSource];
    [self initTableView];
}

-(void)getDataSource
{
    
    _dataArr = [NSMutableArray new];
    NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"MagicCube.plist" ofType:nil];
    NSArray *dataArr = [[NSArray alloc] initWithContentsOfFile:sourcePath];
    [_dataArr addObjectsFromArray:dataArr];
    [_tableView reloadData];
    NSLog(@"dataArr = %@",dataArr);
}

-(void)initTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height) style:UITableViewStyleGrouped];
    [self.view addSubview:_tableView];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section < _dataArr.count) {
        NSDictionary *videoDic = _dataArr[section];
        NSArray *videoList = videoDic[@"List"];
        return videoList.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (indexPath.row < _dataArr.count) {
        NSDictionary *videoDic = _dataArr[indexPath.section];
        NSArray *videoList = videoDic[@"List"];
        NSDictionary *currentVideoInfo = videoList[indexPath.row];
        NSString *videoName = currentVideoInfo[@"Title"];
        videoName = [videoName stringByReplacingOccurrencesOfString:@".mp4" withString:@""];
        cell.textLabel.text = videoName;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section < _dataArr.count) {
        NSDictionary *videoDic = _dataArr[section];
        NSString *sectionTitle = videoDic[@"Name"];
        return sectionTitle;
    }
    return @"";
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *videoDic = _dataArr[indexPath.section];
    NSArray *videoList = videoDic[@"List"];
    NSDictionary *currentVideoInfo = videoList[indexPath.row];
    NSString *videoName = currentVideoInfo[@"Title"];
    NSString *videoPath = [[NSBundle mainBundle] pathForResource:videoName ofType:nil];
    NSLog(@"videoPath = %@",videoPath);
    NSString *currentVideoName = [videoName stringByReplacingOccurrencesOfString:@".mp4" withString:@""];
    VideoViewController *videoVC = [[VideoViewController alloc] init];
    videoVC.navigationItem.title = currentVideoName;
    videoVC.videoPath = videoPath;
    videoVC.videoName = currentVideoName;
    videoVC.videoID = [currentVideoInfo[@"VideoID"] integerValue];
    [self.navigationController pushViewController:videoVC animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
